import api from '../../utils/api';
import { UserInfoType } from '../../types/index';

Component({
  mixins: [],
  data: {
    value: '',
  },
  props: {
    visible: false,
    placeholder: '写点评论...',
    onClosePopup: () => {},
    onAddComment: (data) => {}
  },
  didMount() {},
  didUpdate() {},
  didUnmount() {},
  methods: {
    // 关闭弹出层
    onMaskClick() {
      this.props.onClosePopup();
      this.resetInput();
    },
    // 重置输入框内容
    resetInput() {
      this.setData({ value: '' });
    },
    // 处理输入框内容变化
    bindInputContent(event) {
      this.setData({
        value: event.detail.value
      })
    },
    addComment() {
      // 检查输入内容
      if (this.data.value.trim()) { 
        api.getUserInfo(true).then((res: UserInfoType) => {
          if (res.id) {
            this.props.onAddComment({
              content: this.data.value,
              author: res.id
            })
            this.resetInput();
          }
        })
      } else {
        my.showModal({
          title: '提示',
          content: '评论内容不能为空'
        })
      }
    }
  },
});
